class ApplicationController < ActionController::Base  
  helper :all # include all helpers, all the time
  helper_method :current_user
  filter_parameter_logging :password, :password_confirmation
  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => 'e684b315859a66d7a32762c153c76303'
  
  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  # filter_parameter_logging :password

  I18n.locale = 'pl'

  private

  # SQLITE3... nie wie co to true ani false, za to 'f' oraz 't' zna świetnie.
  # http://chriscraig.net/blog/sqlite-boolean-true-or-false/
  # powyżej dość dobitnie skomentowane "What a pain in the ass this is."

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  end

  def require_user
    unless current_user
      store_location
      flash[:error] = "Należy się zalogować, aby uzyskać dostęp do tej strony"
      redirect_to new_user_session_url
      return false
    end
  end

  def require_no_user
    if current_user
      store_location
      flash[:error] = "Należy się wylogować, aby uzyskać dostęp do tej strony"
      redirect_to :controller => 'users'
      return false
    end
  end

  def require_admin
    if !current_user
      redirect_to :controller => "users", :action => "index"
    else
      require_user
      unless current_user.admin?
        flash[:error] = "Brak dostępu. Zawartość przeznaczona wyłącznie dla administratorów."
        redirect_to :controller => "users", :action => "index"
      end
    end
  end

  def store_location
    session[:return_to] = request.request_uri
  end

  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  def number_to_currency(number, options = {})
    options.symbolize_keys!

    defaults  = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
    currency  = I18n.translate(:'number.currency.format', :locale => options[:locale], :raise => true) rescue {}
    defaults  = defaults.merge(currency)

    precision = options[:precision] || defaults[:precision]
    unit      = options[:unit]      || defaults[:unit]
    separator = options[:separator] || defaults[:separator]
    delimiter = options[:delimiter] || defaults[:delimiter]
    format    = options[:format]    || defaults[:format]
    separator = '' if precision == 0
    begin
      format.gsub(/%n/, number_with_precision(number,
        :precision => precision,
        :delimiter => delimiter,
        :separator => separator)
      ).gsub(/%u/, unit)
    rescue
      number
    end
  end
  
end
